package com.hardy.leetcode;

import java.util.Arrays;

/**
 * 数组中所有的数都出现了3次有一个出现了一次，找出这个数 User: Ruijie Date: 2016/8/12
 */
public class SingleNumberii {

    public static int singleNumberii(int[] A) {
        if(A.length==1){
            return A[0];
        }
        Arrays.sort(A);
        int i,result=A[0];
        for(i=1;i<A.length-1;i++){
            if(A[i]!=A[i-1]&&A[i]!=A[i+1]) {
                result=A[i];
                break;
            }
        }
        if(i==A.length-1&&A[i]!=A[i-1]){
            return A[i];
        }
        return result;
    }

    public static void main(String[] args) {
        int[] array={-4,-4,-4,1,1,1,2,2,2,3};
        int[] array1={1,2,2,2,3,3,3,4,4,4};
        int[] array2={1,1,1,2,3,3,3,4,4,4};
        System.out.println(singleNumberii(array));
        System.out.println(singleNumberii(array1));
        System.out.println(singleNumberii(array2));
    }
}
